﻿using Core.Framework.Model.Common;
using Core.Framework.Redis;
using Core.Framework.Util;
using System;
using System.Collections.Generic;
using System.Text;

namespace Core.IBusiness.IComment.Model
{
    /// <summary>
    /// 信息redis缓存工具箱
    /// </summary>
    public class InformationRedisCache
    {
        /// <summary>
        /// 信息点赞
        /// </summary>
        public readonly string informationStarTableName = "information_Star";
        
        /// <summary>
        /// 信息关注
        /// </summary>
        public readonly string informationWatchTableName = "information_Watch";

        /// <summary>
        /// 用户点赞
        /// </summary>
        public readonly string userStarTableName = "information_User_Star";

        /// <summary>
        /// 用户关注
        /// </summary>
        public readonly string userWatchTableName = "information_User_Watch";

        /// <summary>
        /// redis 仓库
        /// </summary>
        public readonly int redisDB = CoreStartupHelper.GetConfigValue("Redis:Information").ToInt(1);


        /// <summary>
        /// 信息标识
        /// </summary>
        private int key { get; set; }

        /// <summary>
        /// 用户标识
        /// </summary>
        private int userKey { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key">标识列</param>
        /// <param name="userKey">用户标识</param>
        public InformationRedisCache(int key, int userKey)
        {
            this.key = key;
            this.userKey = userKey;
        }


        #region 信息点赞

        #region 赞

        /// <summary>
        /// 点赞评论
        /// </summary>
        public void InfoStar()
        {
            var redis = new RedisHelper(redisDB);
            redis.ListLeftPush(this.UserTableName(this.userStarTableName), key.ToString());
            redis.HashSet(this.HashTableName(this.informationStarTableName), userKey.ToString(), 1);
        }

        /// <summary>
        /// 取消点赞评论
        /// </summary>
        public void InfoUnStar()
        {
            var redis = new RedisHelper(redisDB);
            redis.ListRemove(this.UserTableName(this.userStarTableName), key.ToString());
            redis.HashDelete(this.HashTableName(this.informationStarTableName), userKey.ToString());
        }

        /// <summary>
        /// 获取点赞评论
        /// </summary>
        public List<KeyValuePair<string, string>> GetInfoStar()
        {
            var redis = new RedisHelper(redisDB);

            return redis.HashGetAll(this.HashTableName(this.informationStarTableName)) ?? new List<KeyValuePair<string, string>>();
        }

        /// <summary>
        /// 获取指定用户点赞列表
        /// </summary>
        public List<string> GetInfoUserStar()
        {
            var redis = new RedisHelper(redisDB);
            return redis.ListRange<string>(this.UserTableName(this.userStarTableName)) ?? new List<string>();
        }

        #endregion


        #region 关注

        /// <summary>
        /// 点赞评论
        /// </summary>
        public void InfoWatch()
        {
            var redis = new RedisHelper(redisDB);
            redis.ListLeftPush(this.UserTableName(this.userWatchTableName), key.ToString());
            redis.HashSet(this.HashTableName(this.informationWatchTableName), userKey.ToString(), 1);
        }

        /// <summary>
        /// 取消点赞评论
        /// </summary>
        public void InfoUnWatch()
        {
            var redis = new RedisHelper(redisDB);
            redis.ListRemove(this.UserTableName(this.userWatchTableName), key.ToString());
            redis.HashDelete(this.HashTableName(this.informationWatchTableName), userKey.ToString());
        }

        /// <summary>
        /// 获取点赞评论
        /// </summary>
        public List<KeyValuePair<string, string>> GetInfoWatch()
        {
            var redis = new RedisHelper(redisDB);
            return redis.HashGetAll(this.HashTableName(this.informationWatchTableName)) ?? new List<KeyValuePair<string, string>>();
        }

        /// <summary>
        /// 获取指定用户关注列表
        /// </summary>
        public List<string> GetInfoUserWatch()
        {
            var redis = new RedisHelper(redisDB);
            return redis.ListRange<string>(this.UserTableName(this.userWatchTableName)) ?? new List<string>();
        }
        #endregion


        #endregion



        /// <summary>
        /// 获取HashName
        /// </summary>
        /// <returns></returns>
        private string HashTableName(string table)
        {
            return string.Format("{0}_{1}", table, this.key);
        }

        /// <summary>
        /// 获取UserHashName
        /// </summary>
        /// <returns></returns>
        private string UserTableName(string table)
        {
            return string.Format("{0}_{1}", table, this.userKey);
        }

    }
}
